﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using SRM.DataObjects;
using SRM;

public partial class UserControls_OverseasUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void OverseasPurpose_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        Telerik.Web.UI.RadComboBox overseaspurpose_combobox = null;
        System.Web.UI.WebControls.TextBox others_textbox = null;

        overseaspurpose_combobox = sender as Telerik.Web.UI.RadComboBox;
        others_textbox = overseaspurpose_combobox.Parent.FindControl("others_textbox") as System.Web.UI.WebControls.TextBox;

        System.Web.UI.HtmlControls.HtmlTableRow overseas_other_row = overseaspurpose_combobox.Parent.FindControl("overseas_other_row") as System.Web.UI.HtmlControls.HtmlTableRow;
        
        if (overseas_other_row != null)
        {
            if (overseaspurpose_combobox.SelectedValue == "021")
            {
                overseas_other_row.Visible = true;
                others_textbox.Text = string.Empty;
            }
            else
            {
                overseas_other_row.Visible = false;
                others_textbox.Text = string.Empty;
            }
        }
    } // protected void OverseasPurpose_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.UpdateParameters["startDate"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.UpdateParameters["endDate"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.UpdateParameters["country"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.UpdateParameters["durationOfStay"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.UpdateParameters["purpose"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.InsertParameters["startDate"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.InsertParameters["endDate"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.InsertParameters["country"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.InsertParameters["durationOfStay"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(Overseas_ObjectDataSource.InsertParameters["purpose"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgOverseas_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            Overseas_ObjectDataSource.UpdateParameters["overseasKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("overseasKey"));
            Overseas_ObjectDataSource.UpdateParameters["startDate"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("startdate_datepicker"));
            Overseas_ObjectDataSource.UpdateParameters["endDate"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("enddate_datepicker"));
            Overseas_ObjectDataSource.UpdateParameters["country"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("country_combobox"));
            Overseas_ObjectDataSource.UpdateParameters["durationOfStay"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("durationofstay_combobox"));
            Overseas_ObjectDataSource.UpdateParameters["purpose"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("purpose_combobox"));
            Overseas_ObjectDataSource.UpdateParameters["others"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("others_textbox"));

            if (ValidateFormFields("UPDATE"))
            {
                Overseas_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("OverseasMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            Overseas_ObjectDataSource.DeleteParameters["overseasKey"].DefaultValue = item["OverseasKey_column"].Text;
            Overseas_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            Overseas_ObjectDataSource.InsertParameters["startDate"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("startdate_datepicker"));
            Overseas_ObjectDataSource.InsertParameters["endDate"].DefaultValue = SRM.Help.GetRadDatePickerValue(item.FindControl("enddate_datepicker"));
            Overseas_ObjectDataSource.InsertParameters["country"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("country_combobox"));
            Overseas_ObjectDataSource.InsertParameters["durationOfStay"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("durationofstay_combobox"));
            Overseas_ObjectDataSource.InsertParameters["purpose"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("purpose_combobox"));
            Overseas_ObjectDataSource.InsertParameters["others"].DefaultValue = SRM.Help.GetTextBoxValue(item.FindControl("others_textbox"));

            if (ValidateFormFields("INSERT"))
            {
                Overseas_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("OverseasMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }
    }

    protected void rgOverseas_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            DataTable dt = null;
            GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
            geiEditedItem.Visible = true;

            Telerik.Web.UI.RadDatePicker startdate_datepicker = null;
            Telerik.Web.UI.RadDatePicker enddate_datepicker = null;
            Telerik.Web.UI.RadComboBox country_combobox = null;
            Telerik.Web.UI.RadComboBox durationofstay_combobox = null;
            Telerik.Web.UI.RadComboBox purpose_combobox = null;
            System.Web.UI.WebControls.TextBox others_textbox = null;

            startdate_datepicker = geiEditedItem.FindControl("startdate_datepicker") as Telerik.Web.UI.RadDatePicker;
            enddate_datepicker = geiEditedItem.FindControl("enddate_datepicker") as Telerik.Web.UI.RadDatePicker;
            country_combobox = geiEditedItem.FindControl("country_combobox") as Telerik.Web.UI.RadComboBox;
            durationofstay_combobox = geiEditedItem.FindControl("durationofstay_combobox") as Telerik.Web.UI.RadComboBox;
            purpose_combobox = geiEditedItem.FindControl("purpose_combobox") as Telerik.Web.UI.RadComboBox;
            others_textbox = geiEditedItem.FindControl("others_textbox") as System.Web.UI.WebControls.TextBox;

            // binding of country_combobox, durationofstay_combobox, purpose_combobox
            Help.BindSapCodeToComboBox(country_combobox, SRM_Code.GetCountry("1"));
            Help.BindSapCodeToComboBox(durationofstay_combobox, SRM_Code.GetDurationOfStay("1"));
            Help.BindSapCodeToComboBox(purpose_combobox, SRM_Code.GetPurposeOfStay("1"));

            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                startdate_datepicker.SelectedDate = ((DataRowView)e.Item.DataItem).Row["StartDate"] as DateTime?;
                enddate_datepicker.SelectedDate = ((DataRowView)e.Item.DataItem).Row["EndDate"] as DateTime?;
                //country_combobox.SelectedValue = ((DataRowView)e.Item.DataItem).Row["Country"].ToString();
                //durationofstay_combobox.SelectedValue = ((DataRowView)e.Item.DataItem).Row["DurationOfStay"].ToString();
                //purpose_combobox.SelectedValue = ((DataRowView)e.Item.DataItem).Row["Purpose"].ToString();

                Help.SetComboBoxSelectedValue(country_combobox, ((DataRowView)e.Item.DataItem).Row["Country"].ToString());
                Help.SetComboBoxSelectedValue(durationofstay_combobox, ((DataRowView)e.Item.DataItem).Row["DurationOfStay"].ToString());
                Help.SetComboBoxSelectedValue(purpose_combobox, ((DataRowView)e.Item.DataItem).Row["Purpose"].ToString());
            }

            if (purpose_combobox.SelectedValue == "021")
            {
                // find others row
                System.Web.UI.HtmlControls.HtmlTableRow overseas_other_row = purpose_combobox.Parent.FindControl("overseas_other_row") as System.Web.UI.HtmlControls.HtmlTableRow;

                if (overseas_other_row != null)
                {
                    overseas_other_row.Visible = true;
                    others_textbox.Text = ((System.Data.DataRowView)e.Item.DataItem).Row["Others"].ToString();
                }
                else
                {
                    overseas_other_row.Visible = false;
                    others_textbox.Text = string.Empty;
                }
            } // if (hobbycategory_combobox.SelectedValue == "999")

        } // if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    }
}